package org.example.secondkill.controller;

import org.example.secondkill.domain.User;
import org.example.secondkill.domain.vo.LoginVO;
import org.example.secondkill.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private IUserService userService;

    @PostMapping("/register")
    public Boolean registerAccount(@RequestBody User userVO) {
        return userService.register(userVO.getNickname(), userVO.getPassword());
    }

    @RequestMapping("/login")
    @ResponseBody
    public Boolean doLogin(LoginVO loginVO) {
        try {
            // 登录
            userService.login(loginVO);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 批量生成账号
     */
    @PostMapping("/many_users/{number}")
    @ResponseBody
    public Boolean createManyUsers(@PathVariable("number") int number) {
        try {
            userService.creatManyUsers(number);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
}
